#include "TPM_HAL_GB.h"

uint8 u8TPM_CheckTOF (uint8 u8timer)
{
    if (u8timer == 1)
    {
        return TPM1SC_TOF;
    }
    else
    {
        return TPM2SC_TOF;
    }        
}

void vfnTPM_ClearTOF (uint8 u8timer)
{
    if (u8timer == 1)
    {
        TPM1SC_TOF = 0;
    }
    else
    {
        TPM2SC_TOF = 0;
    }
}

void vfnTPM_PrescalerSelect (uint8 u8timer, uint8 u8prescaler)
{
    if (u8timer == 1)
    {
        TPM1SC_PS = u8prescaler;
    }
    else
    {
        TPM2SC_PS = u8prescaler;
    }
}

void vfnTPM_SetModulo (uint8 u8timer, uint16 u16modulo)
{
    if (u8timer == 1)
    {
        TPM1MOD = u16modulo;
    }
    else
    {
        TPM2MOD = u16modulo;
    }    
}

void vfnTPM_ClockSelect (uint8 u8timer, uint8 u8clock)
{
    if (u8timer == 1)
    {
        TPM1SC_CLKSx = u8clock;
    }
    else
    {
        TPM2SC_CLKSx = u8clock;
    }    
}

uint8 u8TPM_GetTimerCounter (uint8 u8timer)
{
    if (u8timer == 1)
    {
        (void)TPM1CNTH;
        return TPM1CNTL;
    }
    else
    {
        (void)TPM2CNTH;
        return TPM2CNTL;
    }
}

uint8 u8TPM_GetTimerCounterH (uint8 u8timer)
{
    if (u8timer == 1)
    {
        (void)TPM1CNTL;
        return TPM1CNTH;
    }
    else
    {
        (void)TPM2CNTL;
        return TPM2CNTH;
    }     
}

uint16 u16TPM_GetTimerCounter (uint8 u8timer)
{
	if (u8timer == 1)
    {
        return TPM1CNT;
    }
    else
    {
        return TPM2CNT;
    }  	
}

void vfnTPM_InterruptEnable (uint8 u8timer, uint8 u8status)
{
    if (u8timer == 1)
    {
        TPM1SC_TOIE = u8status;
    }
    else
    {
        TPM2SC_TOIE = u8status;
    }
}

void vfnTPM_EdgeAlignedPWM (uint8 u8timer, uint8 u8channel)
{
    if (u8timer == 1)
    {
        TPM1SC_CPWMS = 0;
        if (u8channel == 0)
        {
            TPM1C0SC_MS0x = 2;
            TPM1C0SC_ELS0x = 2;
        }
        else if (u8channel == 1)
        {
            TPM1C1SC_MS1x = 2;
            TPM1C1SC_ELS1x = 2;
        }
        else
        {
            TPM1C2SC_MS2x = 2;
            TPM1C2SC_ELS2x = 2;
        }
    }
    else
    {
        // This section of the driver doesn't need to be implemented unless
        // it is needed
        /*
        TPM2SC_CPWMS = 0;
        if (u8channel == 0)
        {
            TPM2C0SC_MS0x = 2;
            TPM2C0SC_ELS0x = 2;
        }
        else if (u8channel == 1)
        {
            TPM2C1SC_MS1x = 2;
            TPM2C1SC_ELS1x = 2;
        }
        else if (u8channel == 2)
        {
            TPM2C2SC_MS2x = 2;
            TPM2C2SC_ELS2x = 2;
        }
        else if (u8channel == 3)
        {
            TPM2C3SC_MS3x = 2;
            TPM2C3SC_ELS3x = 2;
        }
        else if (u8channel == 4)
        {
            TPM2C4SC_MS4x = 2;
            TPM2C4SC_ELS4x = 2;
        }
        else
        {
            TPM2C5SC_MS5x = 2;
            TPM2C5SC_ELS5x = 2;
        }
        */   
    }   
}

void vfnTPM_SetPWMValue (uint8 u8timer, uint8 u8channel, uint8 u8value)
{
    if (u8timer == 1)
    {
        if (u8channel == 0)
        {   
            TPM1C0VL = u8value;
        }
        else if (u8channel == 1)
        {
            TPM1C1VL = u8value;
        }
        else
        {
            TPM1C2VL = u8value;
        }
    }
    else
    {
        // Unimplemented for timer 2
    }
}












